package com.jn.picasso.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jn.picasso.R;
import com.jn.picasso.base.BaseActivity;
import com.jn.picasso.contract.MainContract;
import com.jn.picasso.entity.MovieEntity;
import com.jn.picasso.presenter.MainPresenter;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements MainContract.IMainView, View.OnClickListener {

    private Button mButton;
    private TextView mText;
    private ImageView mImage;
    private ImageView mImage2;
    private MainContract.IMainPresenter mPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void init() {
        mPresenter = new MainPresenter(this, this);
        mButton = findViewById(R.id.button);
        mText = findViewById(R.id.text);
        mImage = findViewById(R.id.image);
        mImage2 = findViewById(R.id.image2);
        mButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
            mPresenter.getData();
        }
    }

    @Override
    public void onHttpLoading(boolean loading) {

    }

    @Override
    public void onHttpFail(String... msg) {

    }

    @Override
    public void onHttpTimeout() {

    }

    @Override
    public void onHttpTokenExpiration() {

    }

    @Override
    public void showData(MovieEntity movie) {
        StringBuffer sb = new StringBuffer();
        sb.append("名称:").append(movie.getResult().getMov_title()).append("\n");
        sb.append("导演:").append(movie.getResult().getMov_director()).append("\n");
        sb.append("年份:").append(movie.getResult().getMov_year()).append("\n");
        sb.append("类型:").append(movie.getResult().getMov_type()).append("\n");
        sb.append("评分:").append(movie.getResult().getMov_rating()).append("\n");
        sb.append("地区:").append(movie.getResult().getMov_area()).append("\n");
        sb.append("简介:").append(movie.getResult().getMov_intro()).append("\n");
        mText.setText(sb.toString());

        List<Transformation> transformations = new ArrayList<>();
        transformations.add(new GrayTransformation());
        transformations.add(new BlurTransformation(this));

        Picasso.get()
                .load(movie.getResult().getPoster_url())
                //.resize(200, 620)//重置尺寸
                //.centerCrop()//居中裁剪，配合resize使用
                //.centerInside()
                //.rotate(90)//旋转
                //.placeholder(R.mipmap.loding)//未加载出图片时设置的占位图片
                //.noFade()//无过度动画
                //.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)//不缓存
                //.transform(transformations)
                .into(mImage);
        Picasso.get()
                .load(movie.getResult().getMov_pic())
                //.fit()//适应ImageView尺寸
                //.resizeDimen(R.dimen.image_width, R.dimen.image_height) //重置尺寸,不能和fit同时使用
                //.onlyScaleDown()//只有尺寸缩小的时候，resize才生效
                //.noPlaceholder()//不设置占位图片,和placeholder方法不能同时使用
                //.error(R.mipmap.error)
                //.networkPolicy(NetworkPolicy.OFFLINE)
                //.transform(new BlurTransformation(this))
                //.transform(new GrayTransformation())
                .into(mImage2);
    }

    public static class BlurTransformation implements Transformation {

        RenderScript rs;//RenderScript库主要用于图像处理。比如对图片做高斯模糊等

        public BlurTransformation(Context context) {
            super();
            rs = RenderScript.create(context);
        }

        @Override
        public Bitmap transform(Bitmap bitmap) {
            // 创建一个空的Bitmap用于输出
            Bitmap outputBitmap = Bitmap.createBitmap(bitmap);

            // 创建一个模糊效果的RenderScript内核
            ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

            // 将输入的Bitmap转换为Allocation
            Allocation input = Allocation.createFromBitmap(rs, bitmap);
            Allocation output = Allocation.createFromBitmap(rs, outputBitmap);

            // 设置模糊半径，10.0f是模糊度，可以自行调整
            blurScript.setRadius(10.0f);

            // 设置blurScript对象的输入内存
            blurScript.setInput(input);

            // 将输出数据保存到输出内存中
            blurScript.forEach(output);

            // 将输出内存的内容复制到Bitmap中
            output.copyTo(outputBitmap);

            // 释放rs和allocation
            rs.destroy();
            input.destroy();
            output.destroy();
            bitmap.recycle();

            return outputBitmap;
        }

        @Override
        public String key() {
            return "blur";
        }
    }

    public static class GrayTransformation implements Transformation{

        @Override
        public Bitmap transform(Bitmap source) {
            int width, height;
            height = source.getHeight();
            width = source.getWidth();

            Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
            Canvas c = new Canvas(bmpGrayscale);
            Paint paint = new Paint();
            ColorMatrix cm = new ColorMatrix();
            cm.setSaturation(0);
            ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
            paint.setColorFilter(f);
            c.drawBitmap(source, 0, 0, paint);

            if(source!=null && source!=bmpGrayscale){
                source.recycle();
            }
            return bmpGrayscale;
        }

        @Override
        public String key() {
            return "gray";
        }
    }
}